#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
	int socketfd = socket(AF_INET, SOCK_STREAM, 0);
	if(socketfd == -1){
		printf("socket error\n");
		return -1;
	}
	
	struct sockaddr_in serveraddr, clientaddr;
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(2250);
  	serveraddr.sin_addr.s_addr = inet_addr("192.168.220.128");
	if(bind(socketfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0){
		printf("bind error\n");
		return -1;
	}
	
	if(listen(socketfd, 2) < 0){
		printf("listen error\n");
		return -1;
	}
	socklen_t addrlen = sizeof(clientaddr);	
	int acceptfd = accept(socketfd, (struct sockaddr*)&clientaddr, &addrlen);
	if(acceptfd < 0){
		printf("accept error\n");
		return -1;
	}
	printf("connected\n");
	printf("client addr : %s\n",inet_ntoa(clientaddr.sin_addr));
	
	char buf[64];
	while(1){
		if(recv(acceptfd, (void*)buf, sizeof(buf), 0) < 0){
			printf("recv error\n");
			return -1;
		}
		printf("%s", buf);
		if(strncmp(buf, "quit", 4) == 0){
			break;
		}
	}
	close(socketfd);
	close(acceptfd);
	return 0;
}
